<script type="text/javascript">

$(function(){
	//ajax 文件上传
	$("#input").change(function(){
		var formData = new FormData();
		formData.append('name', $('#input')[0].files[0]);
		$.ajax({
			url:'www.xx.com',
			type:'post',
			cache: false,
			data: formData,
			processData: false,
			contentType: false,
			// dataType:'json',
			success:function(data){

			},
			error:function(){

			}
		})
	})
	//计时器
    var timeIndex = 0;
    $(".btnl").click(function(){    //点击btnl触发setTime()函数
        setTime();
        times = setInterval(setTime, 1000);    //每隔1秒执行函数
    });
    $(".btnr").click(function(){
        clearInterval(times);    //清除对函数的调用
    });
    function setTime(){
        var hour = parseInt(timeIndex / 3600);    // 计算时 
        var minutes = parseInt((timeIndex % 3600) / 60);    // 计算分 
        var seconds = parseInt(timeIndex % 60);    // 计算秒  
        hour = hour < 10 ? "0" + hour : hour;
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        $(".showTime").html(hour + ":" + minutes + ":" + seconds);
        timeIndex++;
    }
})
</script>





<?php
public function translate(){
    $url = 'http://api.fanyi.baidu.com/api/trans/vip/translate';
    $q = 'yellow';
    $from = 'en';
    $to = 'zh';
    $appid = '20230902001801858';
    $salt = '1435660288';//随机字符串
    $key = 't_YwRrCPLhbVc_0RKUV8';
    $sign = md5($appid.$q.$salt.$key);
    $params = "q=$q&from=$from&to=$to&appid=$appid&salt=$salt&sign=$sign";
    $ispost = 0;
    dump(json_decode(juhecurl($url, $params, $ispost)));
}

/**
 * 请求接口返回内容
 * @param string $url [请求的URL地址]
 * @param string $params [请求的参数]
 * @param int $ipost [是否采用POST形式]
 * @return  string
 */
function juhecurl($url, $params = false, $ispost = 0)
{
    $httpInfo = array();
    $ch       = curl_init();

    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    if ($ispost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        if ($params) {
            curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }
    $response = curl_exec($ch);
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $response;
}

/**
     * 发送文件到客户端
     * @param string $file
     * @param bool   $delaftersend
     * @param bool   $exitaftersend
     */
    public static function sendToBrowser($file, $delaftersend = true, $exitaftersend = true)
    {
        if (file_exists($file) && is_readable($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment;filename = ' . basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check = 0, pre-check = 0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            if ($delaftersend) {
                unlink($file);
            }
            if ($exitaftersend) {
                exit;
            }
        }
    }

    /**
     * 下载文件
     * @param string $remoteUrl
     * @param bool   $filename
     * @param bool   $file_dir
     */
    public static function download($remoteUrl, $filename = '', $file_dir = '')
    {
        $filename = $filename ? $filename . strrchr($remoteUrl,'.') : substr(strrchr($remoteUrl,'/'),1);
        $all_file_dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $file_dir . '/';
        // curl下载文件
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $remoteUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $fileContents = curl_exec($ch);
        // 检查是否有错误发生
        if(curl_errno($ch)) {
            echo 'cURL 错误：' . curl_error($ch);
        }
        // 关闭 cURL 资源
        curl_close($ch);
        // 将文件保存到本地
        file_put_contents($all_file_dir . $filename, $fileContents);
    }